﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Il2CppAnti
{
    class Paths
    {
        private static readonly string IL2CPP_BIN_PATH = "build\\deploy\\netcoreapp3.1";

        private static readonly string IL2CPP_LIBIL2CPP_PATH = "libil2cpp";

        private static readonly string EXEC_DIRECTORY = System.Environment.CurrentDirectory;

        private static readonly string FRAMEWORK_PATH = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319";

        public string ROOT { get; set; }

        public string UNITY_IL2CPP_DLL
        {
            get
            {
                return Path.Combine(ROOT, IL2CPP_BIN_PATH, "Unity.IL2CPP.dll");
            }
        }

        public string UNITY_IL2CPP_IL
        {
            get
            {
                return Path.Combine(ROOT, IL2CPP_BIN_PATH, "Unity.IL2CPP.il");
            }
        }

        public string METADATA_HEADER
        {
            get
            {
                return Path.Combine(ROOT, IL2CPP_LIBIL2CPP_PATH, "il2cpp-metadata.h");
            }
        }

        public string METADATA_CACHE_CPP
        {
            get
            {
                return Path.Combine(ROOT, IL2CPP_LIBIL2CPP_PATH, "vm\\MetadataCache.cpp");
            }
        }

        public string ILASM_EXE
        {
            get
            {
                return Path.Combine(FRAMEWORK_PATH, "ilasm.exe");
            }
        }

        public string ILDASM_EXE
        {
            get
            {
                return Path.Combine(EXEC_DIRECTORY, "ildasm.exe");
            }
        }

        public string BLOCK_ROOT
        {
            get
            {
                return Path.Combine(EXEC_DIRECTORY, "Block");
            }
        }

        public bool Check()
        {
            bool ret = false;
            do
            {
                if (!CheckDLL())
                {
                    Console.WriteLine($"{UNITY_IL2CPP_DLL} not exist .");
                    break;
                }
                if (!CheckHeader())
                {
                    Console.WriteLine($"{METADATA_HEADER} not exist .");
                    break;
                }
                if (!CheckCPP())
                {
                    Console.WriteLine($"{METADATA_CACHE_CPP} not exist .");
                    break;
                }
                if (!CheckIlasmExe())
                {
                    Console.WriteLine($"{ILASM_EXE} not exist .");
                    break;
                }
                if (!CheckIldasmExe())
                {
                    Console.WriteLine($"{ILDASM_EXE} not exist .");
                    break;
                }
                ret = true;
            } while (false);
            return ret;
        }

        private bool CheckDLL()
        {
            return File.Exists(UNITY_IL2CPP_DLL);
        }

        private bool CheckHeader()
        {
            return File.Exists(METADATA_HEADER);
        }

        private bool CheckCPP()
        {
            return File.Exists(METADATA_CACHE_CPP);
        }

        private bool CheckIlasmExe()
        {
            return File.Exists(ILASM_EXE);
        }

        private bool CheckIldasmExe()
        {
            return File.Exists(ILDASM_EXE);
        }
    }
}
